package cn.nfwx.tv.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.animation.Animation;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import cn.nfwx.tv.R;
import cn.nfwx.tv.base.BaseActivity;
import cn.nfwx.tv.controller.AdvertiseController;
import cn.nfwx.tv.conts.Constants;
import cn.nfwx.tv.events.ToMineItemEvent;
import cn.nfwx.tv.helper.AccountHelper;
import cn.nfwx.tv.util.AnimationUtil;
import cn.nfwx.tv.util.AppUtil;
import cn.nfwx.tv.widget.LoginSmsView;

public class SpalshActivity extends BaseActivity implements Handler.Callback {
    private final int durtime = 3000; //动画长度

    private int count = 3;

    private ImageView iv_advertise;
    private FrameLayout iv_spalsh;
    private TextView tv_spalsh_time;
    private boolean _isshow;

    public static final int KEY_LOGIN = 1;

    public static void startActivity(Context context, int value) {
        Intent intent = new Intent(context, SpalshActivity.class);
        intent.putExtra("key", value);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
        AccountHelper.getInstance().setLiveStats(Constants.STATS_QPOP);
        int value = getIntent().getIntExtra("key", -1);
        if (value == -1) {
            if ((getIntent().getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) != 0) {
                finish();
                return;
            }
            iv_advertise = findViewById(R.id.iv_advertise);
            iv_spalsh = findViewById(R.id.iv_spalsh);
            tv_spalsh_time = findViewById(R.id.tv_spalsh_time);
            count = 3;
            tv_spalsh_time.setText(getString(R.string.splash_time, count));
            tv_spalsh_time.setVisibility(View.GONE);
            tv_spalsh_time.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    goHomeActivity();
                }
            });
            if(AccountHelper.getInstance().isLogin(mContext)){
                _isshow = AdvertiseController.getInstance().loadStar(mContext, iv_advertise);
                iv_advertise.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (_isshow) {
                            if(AdvertiseController.getInstance().gotoStartWeb(SpalshActivity.this)){
                                mHandler.removeCallbacksAndMessages(null);
                                finish();
                            }
                        }
                    }
                });
            }

            mHandler.sendEmptyMessageDelayed(0, durtime);
            Animation animation1 = AnimationUtil.getAlphaAnimation(1.0f, 0.0f, 600, 2400);
            AnimationUtil.startAnimation(iv_spalsh, animation1);
        } else if (value == KEY_LOGIN) {
            goHomeActivity();
        }
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_spalsh;
    }

    @Override
    protected String activityName() {
        return SpalshActivity.class.getSimpleName();
    }

    private Handler mHandler = new Handler(this);

    public void showLogin() {
        findViewById(R.id.ll_login_spalsh).setVisibility(View.VISIBLE);
    }

    public void goHomeActivity() {
        if (AccountHelper.getInstance().isLogin(mContext)) {
            //if (true) {
            Intent intent = new Intent(this, HomeActivity.class);
            startActivity(intent);
            finish();
        } else {
            //test();
            showLogin();
        }
    }

    private void test() {
        ((LoginSmsView) findViewById(R.id.lsv)).autoLogin();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void toMineItemEventBus(ToMineItemEvent event) {
        goHomeActivity();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        AppUtil.fixLeakCanary696(getApplicationContext());
        AppUtil.fixInputMethodManagerLeak(this);
        EventBus.getDefault().unregister(this);
        mHandler.removeCallbacksAndMessages(null);
        mHandler = null;
    }

    @Override
    public boolean handleMessage(Message msg) {
        switch (msg.what) {
            case 0:
                if (_isshow) {
                    tv_spalsh_time.setVisibility(View.VISIBLE);
                    mHandler.sendEmptyMessageDelayed(2, 1000);
                } else {
                    goHomeActivity();
                }
                break;
            case 1:
                goHomeActivity();
                break;
            case 2:
                if (count > 0) {
                    count--;
                    tv_spalsh_time.setText(getString(R.string.splash_time, count));
                    mHandler.sendEmptyMessageDelayed(2, 1000);
                } else {
                    goHomeActivity();
                }
                break;
            default:
                break;
        }
        return false;
    }
}
